#!/usr/bin/env bash
#
# Copyright (C) 2000-2021 Kern Sibbald
# Copyright (C) 2021-2022 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup and test the malware features
#
TestName="malware-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

change_jobname BackupClient1 $JobName
start_test

cat <<EOF >> $conf/bacula-dir.conf
Job {
  Name = VerifyData
  Level = Data
  Type = Verify
  FileSet = "Full Set"
  Storage = File1
  Messages = Standard
  Pool = File
  Priority = 10
  Client = zog8-fd
}
Job {
  Name = VerifyVolCat
  Level = VolumeToCatalog
  Type = Verify
  FileSet = "Full Set"
  Storage = File1
  Messages = Standard
  Pool = File
  Priority = 10
  Client = zog8-fd
}
EOF

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'CheckMalware', 'yes', 'Job')"
export REGRESS_MALWARE_URL=file:$tmp/malware.dump
cat $cwd/scripts/malware.dump >  $tmp/malware.dump
md5sum $cwd/build/po/fr.po | awk '{print $1}'   >> $tmp/malware.dump

ts=`date +%s`
ds=`expr $ts - 10000`
d=`date --date="@$ds" +%m%d%H%M`

ds2=`expr $ts - 90000`
d2=`date --date="@$ds2" +%m%d%H%M`

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File1 pool=File slot=1 drive=0
run job=$JobName yes
wait
messages
run job=$JobName level=full yes
wait
messages
@exec "ls -l $working/malware-MD5.dat"
@exec "touch -m -t '$d' $working/malware-MD5.dat"
@exec "ls -l $working/malware-MD5.dat"
run job=$JobName level=full yes
wait
messages
@exec "ls -l $working/malware-MD5.dat"
@exec "touch -m -t '$d2' $working/malware-MD5.dat"
@exec "ls -l $working/malware-MD5.dat"
run job=$JobName level=full yes
wait
messages
@$out $tmp/log2.out
run job=$JobName level=full comment="job with 0 files" yes
wait
messages
@$out $tmp/log3.out
run job=VerifyData jobid=1 yes
wait
messages
run job=VerifyVolCat jobid=1 yes
wait
messages
list files type=malware jobid=1
@$out $tmp/log2.out
sql
select File.JobId, SourceJobId, Path.Path, Filename, Source from File JOIN Path USING (PathId) JOIN FileEvents USING (JobId, FileIndex);

quit
END_OF_DATA

run_bacula

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MalwareDatabaseCommand', '$tmp/get_malware_toto.ch', 'Director')"
cp $bin/get_malware_abuse.ch $tmp/get_malware_toto.ch
# We execute this command where the handler is located
cp $bin/md5tobase64.py $tmp/

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
reload
messages
@$out $tmp/log1.out
run job=$JobName level=full yes
wait
messages
sql
select File.JobId, SourceJobId, Path.Path, Filename, Source from File JOIN Path USING (PathId) JOIN FileEvents USING (JobId, FileIndex) WHERE FileEvents.JobId = 6;

END_OF_DATA

rm -f $working/malware-MD5.dat

run_bconsole

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MalwareDatabaseCommand', 'get_malware_truc.ch', 'Director')"
cp $bin/get_malware_abuse.ch $bin/get_malware_truc.ch

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
reload
messages
@$out $tmp/log1.out
run job=$JobName level=full yes
wait
messages
sql
select File.JobId, SourceJobId, Path.Path, Filename, Source from File JOIN Path USING (PathId) JOIN FileEvents USING (JobId, FileIndex) WHERE FileEvents.JobId = 7;

END_OF_DATA

rm -f $working/malware-MD5.dat

# TODO: Need to check results

run_bconsole
check_for_zombie_jobs storage=File1
stop_bacula

end_test
